package com.arvato.minipro.service.mall;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.minipro.bean.bean_do.mall.CommodityImgDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_vo.mall.CommodityImgVo;
import com.arvato.common.ftp.FtpTool;
import com.arvato.minipro.mapper.mall.CommodityImgMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/**
 * Describe：商品图片service
 * 
 * @author smallking
 *
 *  2018年2月8日
 */
@Service
@Transactional
public class CommodityImgService {

	@Autowired
	private CommodityImgMapper commodityImgMapper;

	@Autowired
	private FtpTool ftpTool;
	/**
	 * 插入一张图片
	 * @param commodityImg
	 * @return
	 */
	public int insert(CommodityImgDo commodityImgDo) {
		return commodityImgMapper.insert(commodityImgDo);
	}

	/**
	 * 更新一章图片
	 * @param img
	 */
	public int update(CommodityImgDo img) {
		return commodityImgMapper.update(img);
	}

	/**
	 * 根据商品ID查询商品图片(分页)
	 * @param query
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageData listCommodityImgByQuery(Query query, Integer pageNum, Integer pageSize) {
		// 设置分页参数
		PageHelper.startPage(pageNum, pageSize);
		// 查询数据
		List<CommodityImgVo> commodityList = commodityImgMapper.listCommodityImgByQuery(query);
		// 得到分页结果
		PageInfo<CommodityImgVo> pageInfo = new PageInfo<CommodityImgVo>(commodityList);
		return new PageData(pageInfo.getTotal(), pageInfo.getList());
	}
	
	/**
	 * 根据条件查询图片(不分页)
	 * @param query
	 * @return
	 */
	public List<CommodityImgVo> listCommodityImgByQuery(Query query) {
		return commodityImgMapper.listCommodityImgByQuery(query);
	}

	/**
	 * 根据商品ID删除商品图片
	 * @param commodityId
	 * @return
	 */
	public int deleteByCommodityId(String commodityId) {
		Query query = new Query();
		int row = 0;
		query.put("commodityId", commodityId);
		List<CommodityImgVo> commodityImgList = commodityImgMapper.listCommodityImgByQuery(query);
		for(CommodityImgVo commodityImgVo : commodityImgList){
			row += commodityImgMapper.delete(commodityImgVo.getImgId());
			if(row >= 1){
				// 数据库记录删除成功之后 删除磁盘上的图片
				String imgPath = commodityImgVo.getImgPath();
				/*File file = new File(imgPath.split("&")[0]);
				file.delete();*/
				ftpTool.delete(imgPath);
			}
		}
		return row;
	}

	/**
	 * 根据ID查询商品图片
	 * @param imgId
	 * @return 
	 */
	public CommodityImgVo getCommodityImgById(String imgId) {
		return commodityImgMapper.getCommodityImgById(imgId);
	}

	/**
	 * 根据ID删除商品图片
	 * @param imgId
	 * @return
	 */
	public int delete(String imgId) {
		CommodityImgVo commodityImgVo = commodityImgMapper.getCommodityImgById(imgId);
		int row = commodityImgMapper.delete(imgId);
		if(row >= 1){
			// 数据库记录删除成功之后 删除磁盘上的图片
			String imgPath = commodityImgVo.getImgPath();
			/*File file = new File(imgPath.split("&")[0]);
			file.delete();
			*/
			ftpTool.delete(imgPath);
		}
		return row;
	}

}
